package com.medusa.aps.business.modules.demand.controller;


import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.demand.model.input.OrderIncrementParam;
import com.medusa.aps.business.modules.demand.model.input.OrderPageQuery;
import com.medusa.aps.business.modules.demand.model.input.OrderParam;
import com.medusa.aps.business.modules.demand.model.output.OrderInfoPageListModel;
import com.medusa.aps.business.modules.demand.mp.entity.OrderInformation;
import com.medusa.aps.business.modules.demand.mp.entity.PlanOperationProgress;
import com.medusa.aps.business.modules.demand.mp.service.OrderInformationService;
import com.medusa.aps.business.modules.demand.mp.service.PlanOperationProgressService;
import com.medusa.aps.business.modules.modeling.model.input.ProcessDetailParam;
import com.medusa.aps.business.modules.modeling.model.output.ProcessRouteDetailVO;
import com.medusa.aps.business.modules.modeling.mp.service.ProcessRouteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * 订单信息控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:47
 */
@Validated
@RestController
@RequestMapping("/orderInformation")
@Tag(name = "需求订单控制层", description = "需求订单控制层")
@Slf4j
public class OrderInformationController {
    /**
     * 服务对象
     */
    @Resource
    private OrderInformationService orderInformationService;
    @Resource
    private ProcessRouteService processRouteService;

    @Resource
    private PlanOperationProgressService planTaskOperationService;

    /**
     * 分页查询所有数据
     *
     * @param query 查询条件实体对象
     * @return 所有数据
     */
    @PostMapping
    @Operation(summary = "分页查询所有数据", description = "分页查询所有数据")
    public Result<Page<OrderInfoPageListModel>> orderPage(@Valid @RequestBody OrderPageQuery query) {
        return Result.ok(
                this.orderInformationService.orderPage(query)
        );
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/detail")
    @Operation(summary = "通过主键查询单条数据", description = "通过主键查询单条数据")
    public Result<OrderInformation> selectOne(@RequestParam Integer id) {
        return Result.ok(this.orderInformationService.getById(id));
    }

    /**
     * 查询工艺路线详情
     *
     * @param materialCode 物料编码
     */
    @GetMapping("/getProcessRoute")
    @Operation(description = "查询工艺详情", summary = "查询工艺详情")
    public Result<ProcessRouteDetailVO> detail(@RequestParam @Parameter(description = "物料编码") String materialCode) {
        return Result.ok(this.processRouteService.detail(new ProcessDetailParam().setMaterialCode(materialCode)));
    }

    /**
     * 批量新增订单数据
     *
     * @param orderParams 订单参数集合
     * @return 新增结果
     */
    @PostMapping("/create")
    @Operation(summary = "批量新增订单数据", description = "批量新增订单数据")
    public Result<Void> addOrders(@RequestBody @Valid @Size(min = 1, message = "数据只能在1-50行之间", max = 50) List<OrderParam> orderParams) {
        this.orderInformationService.createOrders(orderParams);
        return Result.ok();
    }

    /**
     * 批量修改订单数据
     *
     * @param orderParams 订单参数集合
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "批量修改订单数据", description = "批量修改订单数据")
    public Result<Void> update(@RequestBody @Valid @Size(min = 1, message = "限制同时操作1-50条数据", max = 50) List<OrderParam> orderParams) {
        this.orderInformationService.updateDemandOrders(orderParams);
        return Result.ok();
    }

    /**
     * 更新订单优先级
     *
     * @param orderIncrement 接收实体
     */
    @PutMapping("/update/increment")
    @Operation(summary = "更新订单优先级", description = "更新订单优先级")
    public Result<Void> updateIncrement(@RequestBody @Valid @NotNull(message = "请选择需要编辑的订单") OrderIncrementParam orderIncrement) {
        orderInformationService.updateIncrementDemandOrder(orderIncrement);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除数据", description = "删除数据")
    public Result<Void> delete(@RequestBody @Size(min = 1, max = 50, message = "限制同时操作1-50条数据") Set<Long> ids) {
        this.orderInformationService.removeDemandOrders(ids);
        return Result.ok();
    }

    @GetMapping("/getPlanTasks")
    @Operation(summary = "查询计算进度", description = "查询计算进度")
    public Result<PlanOperationProgress> getPlanTasks(@RequestParam Integer id) {
        return Result.ok(this.planTaskOperationService.getById(id));
    }

}



